﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqModel;
using FoneBuddy.LinqModel.Data;
using FoneBuddy.LinqData;
using System.Globalization;



public partial class Add_Product : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getFilterDetail();
            if (Request.QueryString["Id"] != null)
            {
                productId.InnerText = Request.QueryString["id"].ToString();
                lblHeader.Text = "Edit Product";
                BindData();
            }
            else
            {
                lblHeader.Text = "Add Product";
                productId.InnerText = "0";
            }
        }
    }

    #region save cancel and clear
    protected void Save(object sender, EventArgs e)
    {
        Product product = new Product();
        try
        {
            product = new Product(_dataObject);
            BuildObject(product);
            product.getProductByName();


            if (Request.QueryString["Id"] != null)
            {
                if (product.Id != Convert.ToInt32(Request.QueryString["Id"]) && product.Id != 0)
                {
                    divMessage.InnerHtml = "<strong>Error!</strong> Product is already Exists.";
                    divMessage.Attributes.Add("class", "alert alert-danger");
                    return;
                }
                BuildObject(product);
                product.Id = Convert.ToInt32(Request.QueryString["Id"]);
                product.UpdatedDate = System.DateTime.Now;
                product.IsDeleted = false;
                product.IpUpdated = HttpContext.Current.Request.UserHostAddress;
                product.Update();
            }
            else
            {
                if (product.Id > 0)
                {
                    divMessage.InnerHtml = "<strong>Error!</strong> Product Name is already Exists.";
                    divMessage.Attributes.Add("class", "alert alert-danger");
                    return;
                }
                BuildObject(product);
                product.Code = product.GenerateProductCode();
                product.CreatedDate = System.DateTime.Now;
                product.IpAdded = HttpContext.Current.Request.UserHostAddress;

            }
            Session["ProductImage"] = null;
            //SessoinWrapper.Product = product;
            Session["Product"] = product;
            Response.Redirect("~/Add-ProductImage.aspx");
        }
        catch (Exception ex)
        {
        }
    }

    private void BuildObject(Product Product)
    {

        #region GeneralFeature
        Product.BrandId = Convert.ToInt32(drpBrand.SelectedValue);
        Product.CategoryId = Convert.ToInt32(drpCatgory.SelectedValue);
        Product.Name = txtProductName.Text;
        Product.ColorId = Convert.ToInt32(drpColor.SelectedValue);
        Product.NetworkId = Convert.ToInt32(drpNetwork.SelectedValue);
        if (!string.IsNullOrEmpty(txtReleaseDate.Text))
            Product.ReleaseDate = DateTime.Parse(txtReleaseDate.Text, CultureInfo.GetCultureInfo("en-gb"));
        #endregion

        #region Platform

        Product.OSId = Convert.ToInt32(drpOperatingSystem.SelectedValue);
        Product.Processor = txtProcessor.Text;
        Product.GPU = txtGpu.Text;
        Product.OperatingFrequency = txtOperatingFrequency.Text;
        Product.Java = txtJava.Text;
        Product.Browser = txtBrowser.Text;

        #endregion

        #region Camera
        Product.RearCamera = txtRearCamera.Text;
        Product.FrontCamera = txtFrontCamera.Text;
        Product.CameraFeature = txtCameraFeature.Text;
        Product.Flash = txtFlash.Text;
        Product.Video = txtCameraVideo.Text;
        #endregion

        #region Data & Internet
        Product.Speed_3g_4g = txt3g4g.Text;
        Product.Edge = txtEDGE.Text;
        Product.Gprs = txtGprs.Text;
        Product.Wi_Fi = txtWiFi.Text;
        Product.Bluetooth = txtBluetooth.Text;
        #endregion

        #region Memory
        Product.InternalMemoryId = Convert.ToInt32(drpInternalMemory.SelectedValue);
        Product.ExpandableMemory = txtExpandableMemory.Text;
        Product.Ram = txtRam.Text;
        Product.Rom = txtRom.Text;
        #endregion

        #region Bettery
        Product.BetteryType = txtBetteryType.Text;
        Product.MaxStandBy = txtMaxStandBy.Text;
        Product.Capacity = txtCapacity.Text;
        Product.MusicPlayTime = txtMusicPlayTime.Text;
        Product.TalkTime = txtTalkTime.Text;

        #endregion

        #region Display
        Product.DisplayColor = txtDisplayColor.Text;
        Product.DisplaySize = txtDisplaySize.Text;
        Product.DisplayType = txtDisplayType.Text;
        Product.DisplayFeature = txtDisplayFeature.Text;
        Product.Resolution = txtResolution.Text;

        #endregion

        #region Sound
        Product.AlertType = txtAlertType.Text;
        Product.MMJack = txtMmJack.Text;
        Product.LoudSpeaker = txtLoudSpeaker.Text;

        #endregion

        #region Multimedia
        Product.MusicPlayer = txtMusicPlayer.Text;
        Product.VideoPlayer = txtVideoPlayer.Text;
        Product.Radio = txtRadio.Text;
        #endregion

        #region Other Feature

        Product.SimId = Convert.ToInt32(drpSimOption.SelectedValue);
        Product.SimSize = txtSimSize.Text;
        Product.Sensors = txtSensors.Text;
        Product.SARValue = txtSarValue.Text;
        Product.ImportantApps = txtImportantApps.Text;
        Product.DocumentViewer = txtDocumentViewer.Text;
        #endregion

        #region Body
        Product.FormFactorId = Convert.ToInt32(drpFormFactor.SelectedValue);
        Product.Dimention = txtDimention.Text;
        Product.InputMethodId = Convert.ToInt32(drpInputMethod.SelectedValue);
        Product.ProductWeight = txtWeight.Text;
        #endregion

        #region Connectivity
        Product.Gps = txtGps.Text;
        Product.Usb = txtUsb.Text;
        Product.Messaging = txtMessaging.Text;
        #endregion

    }
    #endregion

    private void getFilterDetail()
    {

        List<FilterDetail> filterdetails = new List<FilterDetail>();
        FilterDetail filterdetail = new FilterDetail();
        filterdetail = new FilterDetail(_dataObject);
        filterdetail.FilterId = (Int32)EnumClass.Enums.Filter.Brand;
        filterdetails = filterdetail.GetAllFilterDetailByFilterId();

        if (filterdetails != null)
        {
            drpBrand.DataSource = filterdetails;
            drpBrand.DataBind();
            drpBrand.Items.Insert(0, new ListItem("Select", "0"));

        }
        filterdetail.FilterId = (Int32)EnumClass.Enums.Filter.Category;
        filterdetails = filterdetail.GetAllFilterDetailByFilterId();

        if (filterdetails != null)
        {
            drpCatgory.DataSource = filterdetails;
            drpCatgory.DataBind();
            drpCatgory.Items.Insert(0, new ListItem("Select", "0"));

        }

        filterdetail.FilterId = (Int32)EnumClass.Enums.Filter.InternalMemory;
        filterdetails = filterdetail.GetAllFilterDetailByFilterId();

        if (filterdetails != null)
        {
            drpInternalMemory.DataSource = filterdetails;
            drpInternalMemory.DataBind();
            drpInternalMemory.Items.Insert(0, new ListItem("Select", "0"));

        }
        filterdetail.FilterId = (Int32)EnumClass.Enums.Filter.InputMethod;
        filterdetails = filterdetail.GetAllFilterDetailByFilterId();

        if (filterdetails != null)
        {
            drpInputMethod.DataSource = filterdetails;
            drpInputMethod.DataBind();
            drpInputMethod.Items.Insert(0, new ListItem("Select", "0"));

        }
        filterdetail.FilterId = (Int32)EnumClass.Enums.Filter.OperatingSystem;
        filterdetails = filterdetail.GetAllFilterDetailByFilterId();

        if (filterdetails != null)
        {
            drpOperatingSystem.DataSource = filterdetails;
            drpOperatingSystem.DataBind();
            drpOperatingSystem.Items.Insert(0, new ListItem("Select", "0"));

        }
      
        filterdetail.FilterId = (Int32)EnumClass.Enums.Filter.Color;
        filterdetails = filterdetail.GetAllFilterDetailByFilterId();
        if (filterdetails != null)
        {
            drpColor.DataSource = filterdetails;
            drpColor.DataBind();
            drpColor.Items.Insert(0, new ListItem("Select", "0"));

        }
        filterdetail.FilterId = (Int32)EnumClass.Enums.Filter.SimType;
        filterdetails = filterdetail.GetAllFilterDetailByFilterId();
        if (filterdetails != null)
        {
            drpSimOption.DataSource = filterdetails;
            drpSimOption.DataBind();
            drpSimOption.Items.Insert(0, new ListItem("Select", "0"));

        }
        filterdetail.FilterId = (Int32)EnumClass.Enums.Filter.Network;
        filterdetails = filterdetail.GetAllFilterDetailByFilterId();
        if (filterdetails != null)
        {
            drpNetwork.DataSource = filterdetails;
            drpNetwork.DataBind();
            drpNetwork.Items.Insert(0, new ListItem("Select", "0"));

        }
        filterdetail.FilterId = (Int32)EnumClass.Enums.Filter.FormFactor ;
        filterdetails = filterdetail.GetAllFilterDetailByFilterId();
        if (filterdetails != null)
        {
            drpFormFactor.DataSource = filterdetails;
            drpFormFactor.DataBind();
            drpFormFactor.Items.Insert(0, new ListItem("Select", "0"));

        }
    }

    private void BindData()
    {
        Product product = new Product();
        product = new Product(_dataObject);
        product.Id = Convert.ToInt32(Request.QueryString["Id"]);
        product.getProductById();

        #region GeneralFeature
        drpBrand.SelectedValue = product.BrandId.ToString();
        drpCatgory.SelectedValue = product.CategoryId.ToString();
        txtProductName.Text = product.Name;
        drpColor.SelectedValue = product.ColorId.ToString();
        drpNetwork.SelectedValue = product.NetworkId.ToString();
        txtReleaseDate.Text = product.ReleaseDate.ToString();
        #endregion

        #region Platform

        drpOperatingSystem.SelectedValue = product.OSId.ToString();
        txtProcessor.Text = product.Processor;
        txtGpu.Text = product.GPU;
        txtOperatingFrequency.Text = product.OperatingFrequency;
        txtJava.Text = product.Java;
        txtBrowser.Text = product.Browser;

        #endregion

        #region Camera
        txtRearCamera.Text = product.RearCamera;
        txtFrontCamera.Text = product.FrontCamera;
        txtCameraFeature.Text = product.CameraFeature;
        txtFlash.Text = product.Flash;
        txtCameraVideo.Text = product.Video;
        #endregion

        #region Data & Internet
        txt3g4g.Text = product.Speed_3g_4g;
        txtEDGE.Text = product.Edge;
        txtGprs.Text = product.Gprs;
        txtWiFi.Text = product.Wi_Fi;
        txtBluetooth.Text = product.Bluetooth;
        #endregion

        #region Memory
        drpInternalMemory.SelectedValue = product.InternalMemoryId.ToString();
        txtExpandableMemory.Text = product.ExpandableMemory;
        txtRam.Text = product.Ram;
        txtRom.Text = product.Rom;
        #endregion

        #region Bettery
        txtBetteryType.Text = product.BetteryType;
        txtMaxStandBy.Text = product.MaxStandBy;
        txtCapacity.Text = product.Capacity;
        txtMusicPlayTime.Text = product.MusicPlayTime;
        txtTalkTime.Text = product.TalkTime;

        #endregion

        #region Display
        txtDisplayColor.Text = product.DisplayColor;
        txtDisplaySize.Text = product.DisplaySize;
        txtDisplayType.Text = product.DisplayType;
        txtDisplayFeature.Text = product.DisplayFeature;
        txtResolution.Text = product.Resolution;

        #endregion

        #region Sound
        txtAlertType.Text = product.AlertType;
        txtMmJack.Text = product.MMJack;
        txtLoudSpeaker.Text = product.LoudSpeaker;

        #endregion

        #region Multimedia
        txtMusicPlayer.Text = product.MusicPlayer;
        txtVideoPlayer.Text = product.VideoPlayer;
        txtRadio.Text = product.Radio; ;
        #endregion

        #region Other Feature

        drpSimOption.SelectedValue = product.SimId.ToString(); ;
        txtSimSize.Text = product.SimSize;
        txtSensors.Text = product.Sensors;
        txtSarValue.Text = product.SARValue;
        txtImportantApps.Text = product.ImportantApps;
        txtDocumentViewer.Text = product.DocumentViewer;
        #endregion

        #region Body
        drpFormFactor.SelectedValue = product.FormFactorId.ToString();
        txtDimention.Text = product.Dimention;
        drpInputMethod.SelectedValue = product.InputMethodId.ToString();
        txtWeight.Text = product.ProductWeight;
        #endregion

        #region Connectivity
        txtGps.Text = product.Gps;
        txtUsb.Text = product.Usb;
        txtMessaging.Text = product.Messaging;
        #endregion
    }

    [System.Web.Services.WebMethod]
    public static string checkProductnameAvailibility(string productname, int Id)
    {
        string productName = productname;
        IDataRepository _dataObject = new DataRepository();
        Product product = new Product();
        product = new Product(_dataObject);
        product.Name = productname;
        product.Id = Id;
        product.getProductByName();
        
        if (product.Id > 0)
            return "true";
        else
            return "false";

    }
}